﻿using GearOptimizer.Adapters;
using GearOptimizer.DataObjects;
using GearOptimizer.Factories;
using StructureMap;

namespace GearOptimizer.Services
{
    public class ItemService
    {
        private readonly IRepositoryFactory repositoryFactory;

        public ItemService()
        {
            repositoryFactory = ObjectFactory.GetInstance<IRepositoryFactory>();
        }

        public Item LoadItem(string itemId)
        {
            var battlenetAdapter = new BattleNetAdapter();
            var itemRepository = repositoryFactory.GetRepository<Item>();
            if (itemRepository.Contains(itemId))
            {
                return itemRepository.Get(itemId);
            }
            var item = battlenetAdapter.RetrieveItem(itemId);
            item.Id = itemId;
            itemRepository.Set(item);
            return item;

        }
    }
}